import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import logger from 'redux-logger'


// reducer 更改state的唯一方式
// dispacth 触发reducer 唯一方式
const reducer = (state, action) => {
    const { type } = action;

    if(type === 'CHANGE_MENU_LIST') {
        return {
            ...state,
            menuList: action.data
        }
    }

    if(type === 'CHANGE_USER_INFO') {
        return {
            ...state,
            userInfo: action.data
        }
    }

    return state
}

// 全局仓库默认值
const initState = {
    // 保存用户信息
    userInfo: {},
    // 二级路由
    menuList: []
}


// 创建从仓库
const store = createStore(reducer, initState, applyMiddleware(thunk, logger))

// 将仓库挂载到window上 用于测试 
window.St = store


export default store